package com.qujia.station.service.impl;

import cn.hutool.core.date.DateUtil;
import com.qujia.common.exception.CustomException;
import com.qujia.common.utils.StringUtils;
import com.qujia.station.domain.Station;
import com.qujia.station.domain.SysAppUserInfo;
import com.qujia.station.domain.dto.SyncUserDto;
import com.qujia.station.mapper.AppUserInfoMapper;
import com.qujia.station.service.IAppUserInfoService;
import com.qujia.station.service.IStationService;
import com.qujia.station.service.UserSyncService;
import com.qujia.system.mapper.AppUserInfoUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;

@Service
@Slf4j
public class UserSyncServiceImpl implements UserSyncService {
    @Resource
    private IStationService stationService;

    @Resource
    private IAppUserInfoService appUserInfoUserService;

    @Resource
    private AppUserInfoMapper appUserInfoMapper;


    @Override
    public Boolean insertUser(SyncUserDto syncUserDto) {
        Boolean res = checkUser(syncUserDto);
        if(!res){
            throw new CustomException("用户已存在");
        }

//        Station station = stationService.selectStationById(Long.valueOf(syncUserDto.getStationId()));
//        if(Objects.isNull(station.getStationId())){
//            throw new CustomException("没有对应的油企");
//        }

        SysAppUserInfo sysAppUserInfo = new SysAppUserInfo();

        String format = new SimpleDateFormat("yyMMdd").format(new Date()) + RandomStringUtils.randomNumeric(5);
        sysAppUserInfo.setMemberCode(format);
        sysAppUserInfo.setMobilePhone(syncUserDto.getMobilePhone());
        sysAppUserInfo.setUserSource("yunhua");
        sysAppUserInfo.setStationId(Long.valueOf(syncUserDto.getEnterpriseId()));
        sysAppUserInfo.setRegisteDate(new Date());
        sysAppUserInfo.setYunhuaUserId(syncUserDto.getYunhuaUserId());
        int i = appUserInfoUserService.insertAppUserInfo(sysAppUserInfo);
        return true;
    }

    @Override
    @Async("threadPoolTaskExecutor")
    public Boolean batchInsertUser(List<SyncUserDto> syncUserDto,CountDownLatch countDownLatch) {
        log.info("开始批量同步用户信息【{}】",syncUserDto);
        try {
            for (SyncUserDto userDto : syncUserDto) {
                if(StringUtils.isEmpty(userDto.getYunhuaUserId()) || StringUtils.isEmpty(userDto.getEnterpriseId())){
                    continue;
                }
                userDto.setUserSource("yunhua");
                userDto.setRegisteDate(new Date());

                if (ObjectUtils.isEmpty(userDto.getMemberCode())) {
                    String format = new SimpleDateFormat("yyMMdd").format(new Date()) + RandomStringUtils.randomNumeric(5);
                    userDto.setMemberCode(format);
                }

                appUserInfoMapper.insertOrUpdateYunHuaUserId(userDto);
            }
//            appUserInfoMapper.batchInsertYunHuaUser(syncUserDto);
        }finally {
            countDownLatch.countDown();
        }
        log.info("开始批量同步用户结束===============");
        return true;
    }

    private Boolean checkUser(SyncUserDto syncUserDto) {

        SysAppUserInfo sysAppUserInfoSelect = new SysAppUserInfo();
        sysAppUserInfoSelect.setYunhuaUserId(syncUserDto.getYunhuaUserId());

        List<SysAppUserInfo> userList = appUserInfoMapper.selectList(sysAppUserInfoSelect);

        if(CollectionUtils.isNotEmpty(userList)){
            return false;
        }
        return true;
    }


}
